package com.sna.snaapplication.controller.UserSystem;



import com.sna.snaapplication.entity.R;
import com.sna.snaapplication.entity.usercore.User;
import com.sna.snaapplication.entity.usercore.core.RegisterRequest;
import com.sna.snaapplication.mapper.UserMapper;
import com.sna.snaapplication.service.LogReg.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Validated//启动注解校验
public class RegisterController {


    @Autowired
    RegisterService registerService;

    @Autowired
    UserMapper userMapper;

    @PostMapping("/register")
    public R register(@RequestBody RegisterRequest request) {

        String username = request.getUsername();
        String password = request.getPassword();
        String confirmpassword = request.getConfirmpassword();

        User u = userMapper.findByUserName(username);
        if (u == null) {
            if (password.equals(confirmpassword)) {
                registerService.reg(username, password);
                return R.ok("注册成功");
            }
            return R.fail("两次密码输入不一致");
        }
        return R.fail("用户已存在");
    }
}
